-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Angular router link tests to use RouterTestingHarness #2430
Conversation
@m-akinc I think I found a fix to the router test issues we talked about this week. Could you buddy the approach in this one test? If it looks good I'll apply it to the other router link tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. The only thing I would suggest is that when updating the rest of the tests, there might be a way to simplify the reuse (and reduce duplication) by introducing a NimbleRouterTestingHarness
that abstracts all the common configuration and stuff like:
public get anchor(): Anchor {
return this.harness.fixture.debugElement.query(By.css('nimble-anchor')).nativeElement as Anchor;
}
...le-angular/src/directives/anchor/tests/nimble-anchor-router-link-with-href.directive.spec.ts
Show resolved
Hide resolved
@m-akinc I've applied the pattern to all the tests and reset your vote. I considered this suggestion but didn't end up using it. For test code I'm ok with a bit more duplication in the interest of readability / transparency so it didn't seem worth it to share just this one line. |
.../directives/anchor-button/tests/nimble-anchor-button-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
...tives/anchor-menu-item/tests/nimble-anchor-menu-item-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
...ar/src/directives/anchor-tab/tests/nimble-anchor-tab-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
...tives/anchor-tree-item/tests/nimble-anchor-tree-item-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
...ectives/breadcrumb-item/tests/nimble-breadcrumb-item-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
...ectives/breadcrumb-item/tests/nimble-breadcrumb-item-router-link-with-href.directive.spec.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Milan Raj <[email protected]>
Pull Request
🤨 Rationale
We have several tests that verify Angular router link behavior by clicking links and inspecting router state. These tests exhibit a couple problems:
[web-server]: 404: /_karma_webpack_/page1?param1=true
The root cause of these issues is that the tests are actually trying to navigate the page when the link is clicked.
👩💻 Implementation
"with-href"
nimbleRouterLink
testsIn researching best practices for writing tests like this I learned that the
RouterTestingModule
we had been using has been deprecated and replaced with a more powerfulRouterTestingHarness
. This blog and video does a good job of explaining it, much better than the angular docs. The basic idea is that the harness sets up a parent component and router which host your component under test. When something tries to navigate the harness captures information about the navigation but doesn't actually navigate the page.The fixes in this PR are to use
RouterTestingHarness
instead ofRouterTestingModule
. This has these side effects:provideRouter
instead ofwithRoutes
/start
and clicking a link topage
resulted in a URL of/start/page
. The simplest fix I found for this was to change the starting page to/
.In addition even if the router doesn't navigate the page we are still invoking click handlers on anchors which try to navigate the page. To address that I'm calling
preventDefault()
from those handlers.error
routerLink
testsThese tests included a
RouterTestingModule
but didn't actually need it orRouterTestingHarness
so I deleted those imports.package.json
Added a couple dev scripts that I found useful while running tests locally. We didn't have an obvious way to debug angular tests or to run just the tests for one project and now we do.
🧪 Testing
✅ Checklist